home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Sample.bin / Click3.java < prev    next >
Text File  |  1998-09-15  |  3KB  |  114 lines

  1.  
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import java.applet.*;
  5.  
  6.  
  7. /** 
  8.  * The final version of the Click applet adds some additional logic
  9.  * to make a pathetic little game out of the parts we've assembled.
  10.  * Note that we've made a significant change to the TargetListener
  11.  * class: it's no longer static.  As an ordinary nested class it has
  12.  * access to the currentTarget field which it writes each time the
  13.  * mouse enters or exits a target.  The MouseListener called shootTarget
  14.  * reads this field when the user clicks the mouse.
  15.  * 
  16.  * This applet runs correctly in HotJava, it requires JDK 1.1.
  17.  */
  18.  
  19.  
  20. public class Click3 extends Applet
  21. {
  22.   Color puckColor = new Color(200, 0, 10);
  23.   Box puck = new Box(puckColor);
  24.   ColumnOfBoxes[] targets = new ColumnOfBoxes[8];
  25.   ColumnOfBoxes currentTarget;
  26.  
  27.   private final class TargetListener 
  28.     extends MouseAdapter implements MouseMotionListener
  29.   {
  30.     private Color newBackground;
  31.     private Color oldBackground;
  32.  
  33.     TargetListener(Color newBackground) {
  34.       this.newBackground = newBackground;
  35.     }
  36.  
  37.     public void mouseEntered(MouseEvent e) {
  38.       oldBackground = e.getComponent().getBackground();
  39.       e.getComponent().setBackground(newBackground);
  40.       currentTarget = (ColumnOfBoxes)(e.getComponent());
  41.     }
  42.  
  43.     public void mouseExited(MouseEvent e) {
  44.       e.getComponent().setBackground(oldBackground);
  45.       currentTarget = null;
  46.     }
  47.  
  48.     private void redispatch(MouseEvent e) {
  49.       Point origin = e.getComponent().getLocation();
  50.       e.translatePoint(origin.x, origin.y);
  51.       e.getComponent().getParent().dispatchEvent(e);
  52.     }
  53.  
  54.     public void mouseMoved(MouseEvent e) {  redispatch(e); }
  55.     public void mouseDragged(MouseEvent e) { redispatch(e); }
  56.     public void mouseClicked(MouseEvent e) { redispatch(e); }
  57.   }
  58.  
  59.   public Click3()
  60.   {
  61.     MouseMotionListener movePuck = new MouseMotionAdapter() {
  62.       public void mouseMoved(MouseEvent e)
  63.       {
  64.     int x = e.getX() - (puck.getSize().width / 2);
  65.     int y = getSize().height - puck.getSize().height;
  66.     puck.setLocation(x, y);
  67.       }
  68.     };
  69.  
  70.     MouseListener shootTarget = new MouseAdapter() {
  71.       public void mouseClicked(MouseEvent e)
  72.       {
  73.     if (currentTarget != null) {
  74.       int nBoxes = currentTarget.getComponentCount();
  75.       if (nBoxes == e.getClickCount()) {
  76.         currentTarget.removeAll();
  77.         currentTarget.getToolkit().beep();
  78.         currentTarget.repaint();
  79.       }
  80.     }
  81.       }
  82.     };
  83.  
  84.     for(int i = 0; i < targets.length; i++) {
  85.       int nBoxes = 1 + (int)(Math.random() * 4.0);
  86.       float boxHue = (float)i / (float)targets.length;
  87.       Color boxColor = Color.getHSBColor(boxHue, 0.5f, 0.85f);
  88.       TargetListener tl = new TargetListener(boxColor.brighter());
  89.       targets[i] = new ColumnOfBoxes(boxColor, nBoxes);
  90.       targets[i].addMouseListener(tl);
  91.       targets[i].addMouseMotionListener(tl);
  92.       add(targets[i]);
  93.     }
  94.  
  95.     add(puck);
  96.     addMouseMotionListener(movePuck);
  97.     addMouseListener(shootTarget);
  98.   }
  99.  
  100.   public static void main(String[] args)
  101.   {
  102.     WindowListener l = new WindowAdapter()
  103.       {
  104.     public void windowClosing(WindowEvent e) {System.exit(0);}
  105.       };
  106.  
  107.     Frame f = new Frame("Click");
  108.     f.addWindowListener(l); 
  109.     f.add(new Click3());
  110.     f.setSize(600, 400);
  111.     f.show();
  112.   }
  113. }
  114.